package com.lgj.core;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.security.OAuth2FeignRequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails;

import java.util.Arrays;

@Configuration
public class OAuth2FeignConfig {
    @Autowired
    private Oauth2Properties properties;

    @Bean
    public OAuth2ClientContext oAuth2ClientContext(){
        return new DefaultOAuth2ClientContext();
    }
    @Bean
    public OAuth2FeignRequestInterceptor oAuth2FeignRequestInterceptor(OAuth2ClientContext oAuth2ClientContext, ResourceOwnerPasswordResourceDetails resourceOwnerPasswordResourceDetails){
        return new OAuth2FeignRequestInterceptor(oAuth2ClientContext,resourceOwnerPasswordResourceDetails);
    }
    @Bean
    public ResourceOwnerPasswordResourceDetails resourceOwnerPasswordResourceDetails(){
        ResourceOwnerPasswordResourceDetails resourceOwnerPasswordResourceDetails = new ResourceOwnerPasswordResourceDetails();
        resourceOwnerPasswordResourceDetails.setUsername(properties.getUsername());
        resourceOwnerPasswordResourceDetails.setPassword(properties.getPassword());
        resourceOwnerPasswordResourceDetails.setClientId(properties.getClientId());
        resourceOwnerPasswordResourceDetails.setClientSecret(properties.getClientSecret());
        resourceOwnerPasswordResourceDetails.setAccessTokenUri(properties.getAccessTokenUri());
        resourceOwnerPasswordResourceDetails.setScope(Arrays.asList(properties.getScope()));
        return resourceOwnerPasswordResourceDetails;
    }
}
